package com.pmcc.core.service;

import com.pmcc.core.config.security.JwtUser;
import com.pmcc.core.entity.security.Employee;
import com.pmcc.core.entity.security.Organization;
import com.pmcc.core.repository.EmployeeRepository;
import com.pmcc.core.repository.OrganizationRepository;
import com.pmcc.core.repository.RegionRepository;
import com.pmcc.core.vo.OrganizationVO;
import com.pmcc.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huihui on 2017/8/16.
 */
@Service
@Transactional
public class OrganizationService {

    @Autowired
    OrganizationRepository  organizationRepository;

    @Autowired
    RegionRepository regionRepository;

    @Autowired
    private EmployeeRepository employeeRepository;


    /**
     * 组织机构查询
     * @return
     */
    public List<OrganizationVO> queryList() {
        JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Employee employee = employeeRepository.findOne(jwtUser.getId());
        List<OrganizationVO> list = new ArrayList<>();
        OrganizationVO vo= this.getOrgVo(employee.getOrganization());
        OrganizationVO orgVo= this.traversalOrg(vo);
        list.add(orgVo);
        return list;
//        return orgVo.getChildren();
    }

    public OrganizationVO traversalOrg(OrganizationVO orgVo){//递归组织表结构
        List<Organization> organizations = organizationRepository.findByParentIdAndDelFlagOrderBySortNoAsc(orgVo.getId(), "0");
        List<OrganizationVO> eleNodeVos = new ArrayList<>();
        if (organizations.size()>0) {
            for (Organization o : organizations){
                OrganizationVO vo= this.getOrgVo(o);
                OrganizationVO eleNodeVo2 = this.traversalOrg(vo);
                eleNodeVos.add(eleNodeVo2);
            }
            orgVo.setChildren(eleNodeVos);
        }
        return orgVo;
    }

    private OrganizationVO getOrgVo(Organization model){
        OrganizationVO vo = new OrganizationVO();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 查询组织机构树treegrid
     *非机构管理使用（不能查询出禁用组织机构）
     * @return
     */
    @Transactional(readOnly = true)
    public List<OrganizationVO> queryTree(String id) {
        List<OrganizationVO> list=null;
        if(StringUtils.isEmpty(id)){
            JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            list= this.getChildrenId(jwtUser.getDeptId());
        }else{
            list =this.getChildren(id);
        }
        if(list!=null && list.size()>0){
            for(OrganizationVO orgVo:list){
                List<OrganizationVO>  childrenList =this.getChildren(orgVo.getId());
                if(childrenList.size()>0){
                    orgVo.setChildren(new ArrayList<>());
                }
            }
        }
        return list;
    }

    /**
     *  传入实体，获取list子节点 vo
     * @param id
     * @return
     */

    public List<OrganizationVO> getChildrenId(String id){
        List<OrganizationVO> children=new ArrayList<>();
        List<Organization> childrenOrg=organizationRepository.findById(id);
        if(childrenOrg.size()>0){
            for (Organization c:childrenOrg) {
                OrganizationVO vo=this.getOrgVo(c);
                children.add(vo);
            }
        }
        return children;
    }

    /**
     *  传入实体，获取list子节点 vo
     * @param id
     * @return
     */

    public List<OrganizationVO> getChildren(String id){
        List<OrganizationVO> children=new ArrayList<>();
        List<Organization> childrenOrg=organizationRepository.findByParentIdAndDelFlagOrderBySortNoAsc(id, "0");
        if(childrenOrg.size()>0){
            for (Organization c:childrenOrg) {
                OrganizationVO vo=this.getOrgVo(c);
                children.add(vo);
            }
        }
        return children;
    }
}
